<div class="layui-body">
    <div style="padding: 16px;">
        <blockquote class="layui-elem-quote layui-text">
            密码修改
        </blockquote>
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">原密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="oldpassword" id="oldpassword" autocomplete="off" value="" lay-affix="eye" class="layui-input" lay-reqtext="请填写原密码" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" id="password" value="" lay-verify="required" autocomplete="off" lay-reqtext="请填写新密码" lay-affix="eye" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认新密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="newpassword" id="newpassword" value="" lay-affix="eye" lay-verify="required" lay-reqtext="确认新密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit lay-filter="demo2">更新</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form'], function() {
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;
        var util = layui.util;
        // 提交事件
        form.verify({
            // 确认密码
            newpassWord: function(value, item) {
                console.log(value);
                var passwordValue = $('#password').val();
                if (value !== passwordValue) {
                    return '两次密码输入不一致';
                } else if (value.length < 6) {
                    return '密码长度不能小于6位';
                }
            }
        });
        form.on('submit(demo2)', function(data) {
            var field = data.field; // 获取表单字段值
            var oldpassword = field.oldpassword;
            var newpassword = field.newpassword;
            if(newpassword == oldpassword){
                layer.msg('新密码不能与原密码相同');
                return false;
            }
            if(newpassword!= field.password){
                layer.msg('两次新密码输入不一致');
                return false;
            }
            if(newpassword.length < 6){
                layer.msg('密码长度不能小于6位');
                return false;
            }
            $.ajax({
                url: '/admin/api/upmypassword.php', // 请求的URL
                type: 'POST', // 请求类型（GET、POST等）
                data: {
                    oldpassword: oldpassword,
                    newpassword: newpassword,
                    password:field.password
                }, // 发送到服务器的数据
                dataType: 'text', // 预期从服务器返回的数据类型
                success: function(response) {
                    // 请求成功时执行的回调函数
                    if(response=="修改成功!"){
                        alert(response);
                        location.href='/admin/api/logout.php?out=true';
                    }else{
                        layer.msg(response);
                    }
                    // console.log(response);
                },
                error: function(error) {
                    // 请求失败时执行的回调函数
                    console.log(error);
                }
            });

                return false;
        });
    });
</script>